//
//  LKBaseNavigationController.m
//  BaseNavigationController
//
//  Created by LIUKUN-PC on 16/4/29.
//  Copyright © 2016年 liukun. All rights reserved.
//

#import "ZXBaseNavigationController.h"
#import "ZXCategories.h"

@interface ZXBaseNavigationController () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIPanGestureRecognizer *panGR;

/**
 *  由于 popViewController 会触发 shouldPopItems，因此用该布尔值记录是否应该正确 popItems
 */
//@property(nonatomic, assign) BOOL shouldPopItemAfterPopViewController;

@end

@implementation ZXBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.layoutMargins = UIEdgeInsetsZero;
    
    [self.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationBar setBarTintColor:[UIColor colorWithHex:0x3bde72]];
    
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}];
    
    //self.shouldPopItemAfterPopViewController = NO;
    
//    if (self.isWebViewNavigation) {
//        return;
//    }
    
    
    // 全屏右滑,自己添加手势,采用系统的代理,以及调用系统的方法;
    
    //系统的私有API
    SEL internalAction = NSSelectorFromString(@"handleNavigationTransition:");
    
    // 1.0 创建手势
    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:internalAction];
    self.panGR = panGR;
    
    // 2.0 设置代理,处理根控制器下的业务
    panGR.delegate = self;
    
    // 3.0 添加手势到导航控制器的view
    [self.view addGestureRecognizer:panGR];
    
    // 4.0 清空原生手势,以此避免不必要bug
    self.interactivePopGestureRecognizer.enabled = NO;
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    //只响应上下30度的右滑
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint tPoint = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:gestureRecognizer.view];
        if (tPoint.x >= 0) {
            CGFloat y = fabs(tPoint.y);
            CGFloat x = fabs(tPoint.x);
            CGFloat af = 30.0f/180.0f * M_PI;
            
            CGFloat tf = tanf(af);
            if ((y/x) <= tf) {
                return YES;
            }
            return NO;
        }else{
            return NO;
        }
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    // 根控制器下,手势无效
    return self.childViewControllers.count >1;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
//    if (self.isWebViewNavigation) {
//        [super pushViewController:viewController animated:animated];
//        return;
//    }
    
    if (!self.viewControllers.count) {
        [super pushViewController:viewController animated:animated];
        return;
    }
    
    //这里是设置了一个返回按钮,效果图如下
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 11, 20)];

    [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];

    //让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //让按钮的内容往左偏移10
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    // 修改导航栏左边的item
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    // 隐藏tabbar
    viewController.hidesBottomBarWhenPushed = YES;

    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

#pragma mark - RxWebViewNavigationViewController

//-(UIViewController*)popViewControllerAnimated:(BOOL)animated{
//    if (self.isWebViewNavigation) {
//        self.shouldPopItemAfterPopViewController = YES;
//    }
//    
//    return [super popViewControllerAnimated:animated];
//}
//
//-(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    if (self.isWebViewNavigation) {
//        self.shouldPopItemAfterPopViewController = YES;
//    }
//    return [super popToViewController:viewController animated:animated];
//}
//
//-(NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated{
//    if (self.isWebViewNavigation) {
//        self.shouldPopItemAfterPopViewController = YES;
//    }
//    return [super popToRootViewControllerAnimated:animated];
//}
//-(BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item{
//    
//    if (!self.isWebViewNavigation) {
//        return YES;
//    }
//    
//    //! 如果应该pop，说明是在 popViewController 之后，应该直接 popItems
//    if (self.shouldPopItemAfterPopViewController) {
//        self.shouldPopItemAfterPopViewController = NO;
//        return YES;
//    }
//    
//    //! 如果不应该 pop，说明是点击了导航栏的返回，这时候则要做出判断区分是不是在 webview 中
//    if ([self.topViewController isKindOfClass:[RxWebViewController class]]) {
//        RxWebViewController* webVC = (RxWebViewController*)self.viewControllers.lastObject;
//        if (webVC.webView.canGoBack) {
//            [webVC.webView goBack];
//            
//            //!make sure the back indicator view alpha back to 1
//            self.shouldPopItemAfterPopViewController = NO;
//            [[self.navigationBar subviews] lastObject].alpha = 1;
//            return NO;
//        }else{
//            [self popViewControllerAnimated:YES];
//            return NO;
//        }
//    }else{
//        [self popViewControllerAnimated:YES];
//        return NO;
//    }
//}


#pragma mark - 横竖屏

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait;
    
}

#pragma mark - Setter

//- (void)setIsWebViewNavigation:(BOOL)isWebViewNavigation {
//    _isWebViewNavigation = isWebViewNavigation;
//    if (!_isWebViewNavigation) {
//        self.shouldPopItemAfterPopViewController = NO;
//        [self.view addGestureRecognizer:self.panGR];
//    } else {
//        [self.view removeGestureRecognizer:self.panGR];
//        
//    }
//    
//    self.interactivePopGestureRecognizer.enabled = _isWebViewNavigation;
//}





@end
